<?php

declare(strict_types=1);

namespace app\admin\model\auth;

use sheep\exception\SheepException;

use sheep\model\Common;

class Role extends Common
{
    protected $name = 'admin_role';

    // 自动数据类型转换
    protected $type = [];

    // 自动 json 转换
    protected $json = [];

    const SUPER_ROLE = 1;

    protected $append = [
        'status_text'
    ];

    public function getRulesAttr($value, $data)
    {
        return explode(',', $value);
    }

    public function setRulesAttr($value, $data)
    {
        return implode(',', $value);
    }

    /**
     * 超级管理组
     *
     * @param [type] $query
     * @return void
     */
    public function scopeSuper($query)
    {
        return $query->where('role_id', self::SUPER_ROLE);
    }

    /**
     * 非超级管理组
     *
     * @param [type] $query
     * @return void
     */
    public function scopeNoSuper($query)
    {
        return $query->where('role_id', '<>', self::SUPER_ROLE);
    }



    public function getIsSuperAttr($value, $data) {
        $value = $value ?: ($data['role_id'] ?? null);
        return $value == self::SUPER_ROLE ? 1 : 0;
    }


}
